const { workerData, parentPort } = require("worker_threads");

const { start, end, imgData, imgData2, Narrow, name, maxW, maxH, w, h } = workerData;

let map = new Map();
let num = 0;

let isFinish = false;

let success = false;

parentPort.on('message', (message) => {
    console.log('LPTEXT','已经结束');
    success=true;
    
});



for (let i = 0; i < maxW; i++) {
    for (let j = 0; j < maxH; j++) {

        if (num < start || num > end) {
            num++;
            continue;
        }

        // console.time('getTempImgData-'+num);
        let tArr = getTempImgData(imgData, w, h, i, j)
        // console.timeEnd('getTempImgData-'+num);

        // console.time('getTempImgData2-'+num);
        // let tArr2 = getTempImgData2(imgData, w, h, i, j)
        // console.timeEnd('getTempImgData2-'+num);


        let reliability = arraysEqual(imgData2, tArr)

        // let obj = turnPos({ maxW, maxH, w, h }, reliability, i, j);

        let obj = turnPos({ maxW, maxH, w, h }, reliability, i, j);

        // map.set(reliability, Object.assign(obj, { TempImg: tArr }));
        map.set(reliability, obj);

        if (reliability >= w * h) {
            isFinish = true;
            break;
        }

        if (success) {
            break;
        }

        num++;
    }
    if (isFinish || success) {
        break;
    }
};

parentPort.postMessage({
    map,
    isFinish
});



/**临时图片数据 */
function getTempImgData(imgData, w, h, wInd, hInd) {
    // 确认提取的区域不超出总行数和列数的范围
    if (wInd + w > imgData.length || hInd + h > imgData[0].length) {
        return [[]]
    }

    const tArr = imgData.slice(wInd, wInd + w).map(row => row.slice(hInd, hInd + h));
    return tArr;
}
function getTempImgData2(imgData, w, h, wInd, hInd) {
    const tArr = new Float32Array(w * h);
  let index = 0;

  for (let i = wInd; i < wInd + w; i++) {
    for (let j = hInd; j < hInd + h; j++) {
      tArr[index++] = imgData[i][j];
    }
  }

  return tArr;
}

// 检查两个数组是否相等
function arraysEqual(arr1, arr2) {
   

    // console.time('compareMatricesWithColorConfidence');
    let results= compareMatricesWithColorConfidence(arr1, arr2, 10);
    // console.timeEnd('compareMatricesWithColorConfidence');

    // console.time('compareMatricesWithColorConfidence_A');
    // compareMatricesWithColorConfidence_A(arr1, arr2, 10);
    // console.timeEnd('compareMatricesWithColorConfidence_A');
    return results;
}

/**屏幕坐标转cocos坐标 */
function turnPos(obj, reliability, wInd, hInd) {
    let oldObj = Object.assign({}, obj)
    // console.log('LPTEXT1',Narrow);

    obj.maxW = obj.maxW + (obj.maxW * Narrow / (100-Narrow));
    obj.maxH = obj.maxH + (obj.maxH * Narrow / (100-Narrow));
    obj.w = obj.w + (obj.w * Narrow / (100-Narrow));
    obj.h = obj.h + (obj.h * Narrow / (100-Narrow));

    //百分比
    let percentageWInd = (Math.round(wInd / oldObj.maxW * 10000) / 100.00);
    let percentageHInd = (Math.round(hInd / oldObj.maxH * 10000) / 100.00);


    wInd = obj.maxW * percentageWInd / 100;
    hInd = obj.maxH * percentageHInd / 100;

    wInd = Math.round(wInd);
    hInd = Math.round(hInd);

    let x = wInd - (obj.maxW / 2);
    x += (obj.w / 2);

    let y = hInd - (obj.maxH / 2);
    y += (obj.h / 2);
    y -= (y * 2)

    return { x, y, reliability, wInd, hInd }
};


function compareMatricesWithColorConfidence_A(matrix1, matrix2, colorThreshold) {
    const rows = matrix1.length;
    const cols = matrix1[0].length;

    if (rows !== matrix2.length || cols !== matrix2[0].length) {
        return 0;
    }

    let matchingPixels = 0;
    

    for (let i = 0; i < rows; i++) {
        for (let j = 0; j < cols; j++) {
            const color1 = matrix1[i][j];
            const color2 = matrix2[i][j];

            if (!(color1[0] == 0 && color1[1] == 0 && color1[2] == 0 && color1[3] == 0)) {
                let squaredDistance = 0;
                for (let k = 0; k < color1.length; k++) {
                    const diff = color1[k] - color2[k];
                    squaredDistance += diff * diff;
                }

                // 使用平方根的近似值
                if ((Math.sqrt(squaredDistance) | 0) <= colorThreshold) {
                    matchingPixels += 1;
                }
            } else {
                matchingPixels += 1;
            }
        }
    }

    return matchingPixels;
}

function compareMatricesWithColorConfidence(matrix1, matrix2, colorThreshold) {
    const rows = matrix1.length;
    const cols = matrix1[0].length;

    if (rows !== matrix2.length || cols !== matrix2[0].length) {
        return 0;
    }

    let matchingPixels = 0;
    
    for (let i = 0; i < rows; i++) {
        for (let j = 0; j < cols; j++) {
            const color1 = matrix1[i][j];
            const color2 = matrix2[i][j];

            // if (!(color1[0] == 0 && color1[1] == 0 && color1[2] == 0 && color1[3] == 0)) {
            if (color1.join(',') != '0,0,0,0') {
                
                let squaredDistance = 0;
                for (let k = 0; k < color1.length; k++) {
                    const diff = color1[k] - color2[k];
                    squaredDistance += diff * diff;
                }

                // 使用平方根的近似值
                if ((Math.sqrt(squaredDistance) | 0) <= colorThreshold) {
                    matchingPixels += 1;
                }
            } else {
                matchingPixels += 1;
            }
        }
    }

    return matchingPixels;
}


